

jsx3.Class.defineClass("jsx3.ext.DualSlider", jsx3.gui.Block, [], function(DualSlider, DualSlider_prototype) {

  DualSlider_prototype.init = function(strName,vntLeft,vntTop,vntWidth,vntHeight) {
    this.jsxsuper(strName,vntLeft,vntTop,vntWidth,vntHeight);
    
    this.jsxminval = 0;
    this.jsxmaxval = 100;
    this.jsxround = 1;
    this.jsxhop = jsx3.Boolean.FALSE;
    this.jsxenabled = jsx3.gui.Form.STATEENABLED;
  };
  
  DualSlider_prototype.initChildren = function() {
    this.getSlider1().constrainValue = DualSlider_prototype.constrainValue1;
    this.getSlider2().constrainValue = DualSlider_prototype.constrainValue2;
  };
  
  DualSlider_prototype.paint = function() {
    this.initChildren();
    this.getSlider1().setEnabled(this.jsxenabled);
    this.getSlider2().setEnabled(this.jsxenabled);
    return this.jsxsuper();
  };
 
  DualSlider_prototype.getValue1 = function() {
    return this.transformValue(this.getSlider1().getValue());
  };
  
  DualSlider_prototype.getValue2 = function() {
    return this.transformValue(this.getSlider2().getValue());
  };
  
  DualSlider_prototype.setValue1 = function(value) {
    var slider = this.getSlider1();
    slider.setValue(this.untransformValue(value));
    this.getText1().setValue(this.transformValue(slider.getValue()));
  };
  
  DualSlider_prototype.setValue2 = function(value) {
    var slider = this.getSlider2();
    slider.setValue(this.untransformValue(value));
    this.getText2().setValue(this.transformValue(slider.getValue()));
  };
  
  DualSlider_prototype.getMinValue = function() {
    return this.jsxminval != null ? this.jsxminval : 0;
  };
  
  DualSlider_prototype.getMaxValue = function() {
    return this.jsxmaxval != null ? this.jsxmaxval : 100;
  };
  
  DualSlider_prototype.getRound = function() {
    return this.jsxround != null ? this.jsxround : 1;
  };
  
  DualSlider_prototype.getHop = function() {
    return this.jsxhop != null ? this.jsxhop : jsx3.Boolean.FALSE;
  };
  
  // transformers
  
  DualSlider_prototype.transformValue = function(value) {
    var min = this.getMinValue();
    var max = this.getMaxValue();
    return min + value * (max - min) / 100;
  };
  
  DualSlider_prototype.untransformValue = function(value) {
    var min = this.getMinValue();
    var max = this.getMaxValue();
    return (value - min) * 100 / (max - min);
  };
  
  // event handlers
  
  DualSlider_prototype.onSlider1Move = function(objSlider, fpValue) {
	var min = this.getMinValue();
    var max = this.getMaxValue();

	if(min != max)
		this.getText1().setValue(this.transformValue(fpValue));
  };
  
  DualSlider_prototype.onSlider2Move = function(objSlider, fpValue) {
	var min = this.getMinValue();
    var max = this.getMaxValue();

	if(min != max)
		this.getText2().setValue(this.transformValue(fpValue));
  };
  
  DualSlider_prototype.onText1Change = function(objText, strValue) {
    if (strValue == null)
      strValue = objText.getValue();
    var slider = this.getSlider1();
    slider.setValue(slider.constrainValue(this.untransformValue(parseFloat(strValue))));
    objText.setValue(this.transformValue(slider.getValue()));
  };
  
  DualSlider_prototype.onText2Change = function(objText, strValue) {
    if (strValue == null)
      strValue = objText.getValue();
    var slider = this.getSlider2();
    slider.setValue(slider.constrainValue(this.untransformValue(parseFloat(strValue))));
    objText.setValue(this.transformValue(slider.getValue()));
  };
  
  DualSlider_prototype.constrainValue1 = function(fpValue) {
    // this -> the jsx3.Slider instance
    var objSlider = this.getAncestorOfType(jsx3.ext.DualSlider);
    var value = objSlider.transformValue(fpValue);
    var round = objSlider.getRound();
    if (round > 0)
      value = Math.round(value / round) * round;
    if (! objSlider.getHop())
      value = Math.min(value, objSlider.getValue2());
    return Math.max(0, Math.min(100, objSlider.untransformValue(value)));
  };
  
  DualSlider_prototype.constrainValue2 = function(fpValue) {
    // this -> the jsx3.Slider instance
    var objSlider = this.getAncestorOfType(jsx3.ext.DualSlider);
    var value = objSlider.transformValue(fpValue);
    var round = objSlider.getRound();
    if (round > 0)
      value = Math.round(value / round) * round;
    if (! objSlider.getHop())
      value = Math.max(value, objSlider.getValue1());
    return Math.max(0, Math.min(100, objSlider.untransformValue(value)));
  };
  
  // getters for children objects
  
  DualSlider_prototype.getSlider1 = function() {
    if (this._jsxslider1 == null)
      this._jsxslider1 = this.getDescendantsOfType(jsx3.gui.Slider)[0];
    return this._jsxslider1;
  };

  DualSlider_prototype.getSlider2 = function() {
    if (this._jsxslider2 == null)
      this._jsxslider2 = this.getDescendantsOfType(jsx3.gui.Slider)[1];
    return this._jsxslider2;
  };

  DualSlider_prototype.getText1 = function() {
    if (this._jsxtext1 == null)
      this._jsxtext1 = this.getDescendantsOfType(jsx3.gui.TextBox)[0];
    return this._jsxtext1;
  };

  DualSlider_prototype.getText2 = function() {
    if (this._jsxtext2 == null)
      this._jsxtext2 = this.getDescendantsOfType(jsx3.gui.TextBox)[1];
    return this._jsxtext2;
  };
    
});
